<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 * @version 1.0
 * @author xpl0it
 */
class Customer extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('authmodel', 'authen');
        $this->load->model('customer_model', 'customer');
        $this->load->model('customer_area_model');
        //$this->output->enable_profiler(TRUE);
    }

    function index() {
        $this->checkauth();
        $data['query'] = $this->customer->select_by_table('customer');
        $content = $this->load->view('customer/index', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render_dt', $content_data);
    }

    function add() { //เพิ่มข้อมูล
        $this->checkauth();

        $data['url'] = base_url() . 'index.php/admin/customer/save';
        $data['title'] = 'เพิ่มข้อมูลผู้ใช้บริการ TOT FTTx';
        $content = $this->load->view('customer/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function edit($id) { //แก้ไขข้อมูล
        $this->checkauth();

        $data['url'] = base_url() . 'index.php/admin/customer/save';
        $data['title'] = 'แก้ไขข้อมูลผู้ใช้บริการ TOT FTTx';
        $data['user'] = $this->customer->select_by_filter('customer', $id);
        $data['area'] = $this->customer_area_model->select_by_filter($id);

        $content = $this->load->view('customer/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function profile($id) {
        $this->checkauth();
        $data['user'] = $this->customer->select_by_filter('customer', $id);
        $data['area'] = $this->customer_area_model->select_by_filter($id);
        $data['url'] = base_url() . 'index.php/admin/customer_report/preview';
        $data['title'] = 'รายละเอียผู้ใช้บริการ TOT FTTx';
        $data['readonly'] = TRUE;
        $data['profile'] = TRUE;

        $content = $this->load->view('customer/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function save() { //บันทึกข้อมูล
        $this->checkauth();
        $data = $this->input->post(NULL, TRUE);

        if (empty($data['cm_id'])) {
            $rs = $this->customer->insert_user($data);
            if ($rs) {
                $data['cm_id'] = $rs;
                $this->customer_area_model->insert($data); // เพิ่มข้อมูล customer_area
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("บันทึกข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/customer") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถบันทึกข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        } else {
            $rs = $this->customer->update_user($data);
            if ($rs) {
                $this->customer_area_model->update($data);
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("แก้ไขข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/customer") . '";</script>';
                echo $data;
            }
        }
    }

    function del($id) {
        $this->checkauth();
        $rs = $this->customer->delete_user('customer', $id);
        if ($rs) {
            $this->customer_area_model->delete($id);
            $this->load->view('template/header');
            $data = '<script type="text/javascript">alert("ลบข้อมูลผู้ใช้สำเร็จแล้ว ! ");location = "' . base_url("index.php/admin/customer") . '";</script>';
            echo $data;
        }
    }

    private function checkauth() {
        if (!$this->session->userdata('authen')) {
            redirect('admin/login/', 'refresh');
        }
    }

}

/* End of file customer.php */
/* Location: ./application/controllers/admin/customer.php */